package com.weasing.res.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.weasing.res.dao.IWxAdminDao;
import com.weasing.res.dao.IWxPrivilegeDao;
import com.weasing.res.dao.IWxRoleDao;
import com.weasing.res.dao.base.impl.BaseDaoImpl;
import com.weasing.res.domain.WxAdmin;
import com.weasing.res.domain.WxPrivilege;
import com.weasing.res.domain.WxRole;

@Repository
public class WxPrivilegeDaoImpl extends BaseDaoImpl<WxPrivilege>
		implements IWxPrivilegeDao {
	@Autowired
	private IWxAdminDao wxAdminDao;
	
	@Autowired
	private IWxRoleDao wxRoleDao;

	/**
	 * 通过用户id找到用户的所有权限
	 * 
	 * @author 黄龙
	 */
	public List<WxPrivilege> findPrivilegeByWxAdminId(WxAdmin wxAdmin) {
		Integer userId = wxAdmin.getRoleid();
		WxRole wxRole = wxRoleDao.findById(userId);

		Integer id = wxRole.getId();
		String hql = "SELECT DISTINCT p FROM WxPrivilege p LEFT OUTER JOIN p.roles"
				+ " r  WHERE r.id = ?";
		List<WxPrivilege> list = (List<WxPrivilege>) this.getHibernateTemplate()
				.find(hql, id);
		return list;
	}

	/**
	 * 通过角色id找到角色的所有权限
	 * 
	 * @author 黄龙
	 */
	public List<WxPrivilege> findPrivilegeByRoleId(Integer roleId) {

		String hql = "SELECT DISTINCT p FROM WxPrivilege p LEFT OUTER JOIN p.roles"
				+ " r  WHERE r.id = ?";
		List<WxPrivilege> list = (List<WxPrivilege>) this.getHibernateTemplate()
				.find(hql, roleId);
		return list;
	}


}
